// ignore_for_file: prefer_const_constructors, prefer_const_literals_to_create_immutables
import 'package:flutter/material.dart';
import 'package:flutter/services.dart';

import 'package:junyeyishu/utils/utils.dart';
import 'package:junyeyishu/widgets/QGWidget.dart';
import 'package:junyeyishu/widgets/QGButton.dart';
import 'dart:math';

class QGOrderConfirmPage extends StatefulWidget {
  dynamic args;
  QGOrderConfirmPage({super.key, this.args});
  @override
  State<QGOrderConfirmPage> createState() => QGOrderConfirmPageState();
}

class QGOrderConfirmPageState extends State<QGOrderConfirmPage> {
  String payType = "WALLET_PAY";

  dynamic data;
  @override
  void initState() {
    super.initState();
    data = widget.args;
  }

  @override
  Widget build(BuildContext context) {
    return QGScaffold(
        title: '确认订单',
        bgColor: Color(0XFF0c1012),
        body: Column(
          children: [
            Expanded(
              child: Stack(
                children: [
                  Padding(
                    padding: EdgeInsets.all(10.rpx),
                    child: ListView(
                      children: [
                        Column(
                          children: [
                             ClipRRect(
                              borderRadius: BorderRadius.circular(6.rpx),
                              child: AspectRatio(
                                aspectRatio: 16 / 9,
                                child: QGImage(url: data?["thumb_img"]),
                              ),
                            ),
                            SizedBox(height: 20.rpx),
                            buildHeader(),
                            SizedBox(height: 20.rpx),
                            buildExtra(),
                            SizedBox(height: 65.rpx + max(10, kSafeBottom)),
                          ],
                        ),
                      ],
                    ),
                  ),
                  Positioned(
                    bottom: 0,
                    left: 0,
                    right: 0,
                    child: buildBottom(),
                  )
                ],
              ),
            ),
          ],
        ));
  }

  buildHeader() {
    return Container(
      padding: EdgeInsets.all(20.rpx),
      decoration: BoxDecoration(
        borderRadius: BorderRadius.circular(10.rpx),
        color: Color(0xFF232A32),
      ),
      child: Column(crossAxisAlignment: CrossAxisAlignment.start, children: [
       
        Text(
          data?["name"] ?? "",
          style: TextStyle(
              color: Color(0xFFffffff),
              fontSize: 16.rpx,
              fontWeight: FontWeight.bold),
        ),
        SizedBox(height: 20.rpx),
        buildItem("创作者", data?["artist_name"]),
        buildItem("合约地址", getshortAddress(data?["contract_address"] ?? ""),
            isCopy: true),
        buildItem("订单金额", "￥${data?["order_price"]}"),
      ]),
    );
  }

  buildItem(title, value, {isCopy = false}) {
    return GestureDetector(
      onTap: () {
        if (isCopy) {
          Clipboard.setData(
              ClipboardData(text: data?["contract_address"] ?? " "));
          toast("复制成功");
        }
      },
      child: Container(
        color: Colors.transparent,
        padding: EdgeInsets.symmetric(vertical: 10.rpx),
        child: Row(
          mainAxisAlignment: MainAxisAlignment.spaceBetween,
          children: [
            Text(
              title ?? "",
              style: TextStyle(
                  color: Color(0xFFffffff),
                  fontSize: 14.rpx,
                  fontWeight: FontWeight.normal),
            ),
            Container(
              color: Colors.transparent,
              child: Row(
                children: [
                  Text(
                    value ?? "",
                    style: TextStyle(
                        color: Color(0xFF999999),
                        fontSize: 14.rpx,
                        fontWeight: FontWeight.normal),
                  ),
                  SizedBox(width: 5.rpx),
                  isCopy ? QGImage(asset: "home_copy") : Container(),
                ],
              ),
            ),
          ],
        ),
      ),
    );
  }

  buildPayType() {
    return Container(
      padding: EdgeInsets.all(20.rpx),
      decoration: BoxDecoration(
        borderRadius: BorderRadius.circular(10.rpx),
        color: Color(0xFFFFFFFF),
      ),
      child: Column(
        crossAxisAlignment: CrossAxisAlignment.start,
        children: [
          Text(
            '支付方式',
            style: TextStyle(
                color: Color(0xFF111111),
                fontSize: 16.rpx,
                fontWeight: FontWeight.bold),
          ),
          SizedBox(height: 20.rpx),
          Row(
            mainAxisAlignment: MainAxisAlignment.spaceEvenly,
            children: [
              Expanded(
                child: GestureDetector(
                  onTap: () {
                    payType = "YJZF";
                    setState(() {});
                  },
                  child: Container(
                    margin: EdgeInsets.only(right: 10.rpx),
                    height: 40.rpx,
                    decoration: payType == "YJZF"
                        ? BoxDecoration(
                            gradient: const LinearGradient(
                              begin: Alignment.centerLeft,
                              end: Alignment.centerRight,
                              colors: [Color(0xFFFFF25A), Color(0xFFDCFF72)],
                            ),
                            borderRadius: BorderRadius.circular(10.rpx),
                          )
                        : BoxDecoration(
                            borderRadius: BorderRadius.circular(10.rpx),
                            color: Color(0xFFF8F8F6),
                          ),
                    child: Column(
                      mainAxisAlignment: MainAxisAlignment.center,
                      children: [
                        Text(
                          '银行卡',
                          style: TextStyle(
                              color: Color(0xFF111111),
                              fontSize: 14.rpx,
                              fontWeight: FontWeight.normal),
                        ),
                      ],
                    ),
                  ),
                ),
              ),
              Expanded(
                child: GestureDetector(
                  onTap: () {
                    payType = "WALLET_PAY";
                    setState(() {});
                  },
                  child: Container(
                    margin: EdgeInsets.only(left: 10.rpx),
                    height: 40.rpx,
                    decoration: payType == "WALLET_PAY"
                        ? BoxDecoration(
                            gradient: const LinearGradient(
                              begin: Alignment.centerLeft,
                              end: Alignment.centerRight,
                              colors: [Color(0xFFFFF25A), Color(0xFFDCFF72)],
                            ),
                            borderRadius: BorderRadius.circular(10.rpx),
                          )
                        : BoxDecoration(
                            borderRadius: BorderRadius.circular(10.rpx),
                            color: Color(0xFFF8F8F6),
                          ),
                    child: Column(
                      mainAxisAlignment: MainAxisAlignment.center,
                      children: [
                        Text(
                          '钱包A',
                          style: TextStyle(
                              color: Color(0xFF111111),
                              fontSize: 14.rpx,
                              fontWeight: FontWeight.normal),
                        ),
                        SizedBox(height: 3.rpx),
                        Text(
                          '更快更方便 管理费10元/月',
                          style: TextStyle(
                              color: payType == "WALLET_PAY"
                                  ? Color(0xFF111111)
                                  : Colors.red,
                              fontSize: 8.rpx,
                              fontWeight: FontWeight.normal),
                        ),
                      ],
                    ),
                  ),
                ),
              ),
            ],
          )
        ],
      ),
    );
  }

  buildExtra() {
    return Row(
      children: [
        Text(
          """注：1.您1小时之内只能取消3次订单，超出次数将限购1小时；
        2.一个用户同一时间仅允许存在一笔待支付订单；
        3.若您短时间未支付违约订单过多，将会被禁止购买一段时间；""",
          style: TextStyle(
              height: 1.5,
              color: const Color(0xffcecece),
              fontSize: 11.rpx,
              fontWeight: FontWeight.bold),
        ),
      ],
    );
  }

  buildBottom() {
    return Container(
      padding: EdgeInsets.only(
          left: 10.rpx,
          right: 10.rpx,
          bottom: max(10, kSafeBottom),
          top: 10.rpx),
      decoration: BoxDecoration(
        color: Color(0xFF232A32),
      ),
      child: Row(mainAxisAlignment: MainAxisAlignment.spaceBetween, children: [
        Text(
          "￥${data?["order_price"]}",
          style: TextStyle(
              color: Color(0xFFffffff),
              fontSize: 24.rpx,
              fontWeight: FontWeight.bold),
        ),
        Flexible(
          child: Container(
              alignment: Alignment.center,
              height: 44.rpx,
              width: 210.rpx,
              child: QGButton(
                title: "立即购买",
                onTap: onPay,
              )),
        )
      ]),
    );
  }

  onPay() {
    var param = {};
    param['pid'] = data?["pid"];
    param['num'] = data?["num"];
    param['pay_type'] = "HUIFU";
    Map<String, dynamic> stringMap = param.cast<String, dynamic>();

      print(stringMap);

    Network.post(url_huifu_generateOrder, data: stringMap, context: context)
        .then((res) async {
      if (!res.success) {
        toast(res.msg);
        return;
      }

      var url = res.data["pay_url"];
      if (url is String && !url.startsWith("http")) {
        // url = decrypt(url);
        url = url;
      }
      if (url is! String || !url.startsWith("http")) {
        toast("数据解析失败");
        return;
      }

      // Navigator.of(context)
      //     .pushReplacementNamed(PageQGPayWebView, arguments: url);

          Navigator.of(context)
          .pushNamed(PageQGPayWebView, arguments: url);
    });
  }
}
